package com.ruoyi.business.controller;

import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.BusZhncOutput;
import com.ruoyi.business.service.IBusZhncOutputService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 大田作物产值Controller
 *
 * @author ruoyi
 * @date 2023-08-01
 */
@RestController
@RequestMapping("/zhnc/output")
@Api(tags = "智慧农场-大田作物产值")
public class BusZhncOutputController extends BaseController {
    @Autowired
    private IBusZhncOutputService busZhncOutputService;


    /**
     * 查询大田作物产值列表
     */
    @GetMapping("/data")
    @ApiOperation("图表数据")
    public AjaxResult data(BusZhncOutput busZhncOutput) {
        startPage();
        List<BusZhncOutput> list = busZhncOutputService.selectBusZhncOutputList(busZhncOutput);
        Map<String, List> map = new HashMap<>();
        List<String> keyList = new ArrayList<>();
        List<Object> yuluxiangList = new ArrayList<>();
        List<Object> hupingzaoList = new ArrayList<>();
        List<Object> qilinxiguaList = new ArrayList<>();
        List<Object> pingguoList = new ArrayList<>();
        List<Object> yingtaoList = new ArrayList<>();

        list.forEach(o -> {
            Date time = o.getTime();
            String t = DateUtils.parseDateToStr(DateUtils.YYYYMM_NIAN, time);
            keyList.add(t);
            yuluxiangList.add(o.getYuluxiang());
            hupingzaoList.add(o.getHupingzao());
            qilinxiguaList.add(o.getQilinxigua());
            pingguoList.add(o.getPingguo());
            yingtaoList.add(o.getYingtao());

        });

        Collections.reverse(keyList);
        Collections.reverse(yuluxiangList);
        Collections.reverse(hupingzaoList);
        Collections.reverse(qilinxiguaList);
        Collections.reverse(pingguoList);
        Collections.reverse(yingtaoList);

        map.put("keyList", keyList);
        map.put("yuluxiangList", yuluxiangList);
        map.put("hupingzaoList", hupingzaoList);
        map.put("qilinxiguaList", qilinxiguaList);
        map.put("pingguoList", pingguoList);
        map.put("yingtaoList", yingtaoList);
        return AjaxResult.success(map);
    }


    /**
     * 查询大田作物产值列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BusZhncOutput busZhncOutput) {
        startPage();
        List<BusZhncOutput> list = busZhncOutputService.selectBusZhncOutputList(busZhncOutput);
        return getDataTable(list);
    }

    /**
     * 导出大田作物产值列表
     */
    @PreAuthorize("@ss.hasPermi('zhnc:output:export')")
    @Log(title = "大田作物产值", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusZhncOutput busZhncOutput) {
        List<BusZhncOutput> list = busZhncOutputService.selectBusZhncOutputList(busZhncOutput);
        ExcelUtil<BusZhncOutput> util = new ExcelUtil<BusZhncOutput>(BusZhncOutput.class);
        util.exportExcel(response, list, "大田作物产值数据");
    }

    /**
     * 获取大田作物产值详细信息
     */
    @PreAuthorize("@ss.hasPermi('zhnc:output:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(busZhncOutputService.selectBusZhncOutputById(id));
    }

    /**
     * 新增大田作物产值
     */
    @PreAuthorize("@ss.hasPermi('zhnc:output:add')")
    @Log(title = "大田作物产值", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusZhncOutput busZhncOutput) {
        return toAjax(busZhncOutputService.insertBusZhncOutput(busZhncOutput));
    }

    /**
     * 修改大田作物产值
     */
    @PreAuthorize("@ss.hasPermi('zhnc:output:edit')")
    @Log(title = "大田作物产值", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusZhncOutput busZhncOutput) {
        return toAjax(busZhncOutputService.updateBusZhncOutput(busZhncOutput));
    }

    /**
     * 删除大田作物产值
     */
    @PreAuthorize("@ss.hasPermi('zhnc:output:remove')")
    @Log(title = "大田作物产值", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(busZhncOutputService.deleteBusZhncOutputByIds(ids));
    }
}
